<?php
        require_once('paths.inc');
        require_once('page.inc');
        require_once('utils.inc');
        require_once(INCLUDE_PATH.'constants.inc');
        require_once(DB_PATH.'db.inc');
        session_start();
        checkIsLoggedIn();
        
		$db = db_connect();
		select_language(); 
			
		$id_comanda = $_GET['index'];  // id-ul comenzii din indexul de comenzi din sesiune
		$moneda_comanda = null;
		
        for ($i = 0; $i < count($currencies); $i++)
            $monedaValues[] = $i;
        $serie = null;
        
		if (isset($id_comanda) && !empty($id_comanda)) {                 
			try {       
				$comanda = Comanda::queryLoadById($db, $id_comanda, $_SESSION['user_logat']->id_client);
				$moneda_comanda = $comanda->moneda_sol;
				$serie = $comanda->nr_factura_sol;
                
                $factura_existenta = false;
                // Incarcare factura
                $factura = Factura::queryLoadFacturaByCommandId($db, $id_comanda);
                if ($factura == CT_NO_FACTURA_FOUND)
                    $factura = new Factura($id_comanda, '', '', '', '', '', '', '', '', '', '', $_SESSION['user_logat']->id_user);
                else
                    $factura_existenta = true;
			}
			catch (QueryLoadComandaException $e) {
				echo "ERROR_PRINT_FACTURA";
				exit();
			}
            catch (QueryLoadFacturaException $e) {
                echo "ERROR_PRINT_FACTURA";
                exit();
            }
		}
		else {
			exit();
		}
		echo "<div align=\"center\">";
			echo '<table align="center" cellpadding="3" cellspacing="0" border="0" id="wrapper2">';
				echo "<thead>";
					echo "<tr>";
						echo "<th colspan=\"4\">";
							echo LANG_Save_and_print_factura;
						echo "</th>";
					echo "</tr>";				
				echo "</thead>";
				
				echo '<tr class="capTabel">';       
					echo '<td id="td_moneda_comanda" align="center" colspan="4">
                            <b>'.LANG_Valoarea.': </b>'.$comanda->suma_totala_sol." ".$currencies[$moneda_comanda].
                            '<span id="moneda_comanda" style="display:none">'.$moneda_comanda.'</span></td>';
				echo '</tr>';
				
				echo '<tr class="capTabel">';
					echo '<td id="td_moneda_factura" align="left"><b>'.LANG_Moneda_facturii.'</b></td><td align="left">';
                    render_combobox("moneda", $monedaValues, $currencies, $factura->moneda, '', 
                    'onChange = "changeCurrencyLabels();schimbaMonedaFacturii();"');
				    echo '</td>';				                                                                                               
                    echo '<td id="td_nume_delegat" align="left"><b>'.LANG_Nume_delegat.CT_OBLIGATORIU.'</b></td>';
                    echo '<td align="left"><input type="text" value="'.$factura->nume_delegat.'" name="nume_delegat" id="nume_delegat" /></td>';
                echo '</tr>';
				
				echo '<tr class="capTabel">';
					echo '<td id="td_curs" align="left"><b>'.LANG_Curs_valutar.'<br /> <span id="monedaComanda">'. $currencies[$moneda_comanda] .'</span> -> <span id="monedaFactura1">'. $currencies[$moneda_comanda] .'</span></b></td>';
                    $disabled = '';
                    if ($factura->moneda == $moneda_comanda)
                        $disabled = 'disabled = "disabled"';
					echo '<td align="left"><input type="text" value="'.$factura->curs_1.'" name="curs" id="curs" class="input_number" '.$disabled.'/></td>';
                    echo '<td id="td_mijloc_transport" align="left"><b>'.LANG_Mijloc_transport.CT_OBLIGATORIU.'</b></td>';
                    echo '<td align="left"><input type="text" value="'.$factura->mijloc_transport.'" name="mijloc_transport" id="mijloc_transport" /></td>';
				echo '</tr>';
				
				echo '<tr class="capTabel">';
					echo '<td align="center" colspan="2"><b>'.LANG_Sau.'</b></td>';
                    echo '<td id="td_cnp" align="left"><b>'.LANG_CNP.CT_OBLIGATORIU.'</b></td>';
                    echo '<td align="left"><input type="text" value="'.$factura->cnp_delegat.'" name="cnp" id="cnp"/></td>';
				echo '</tr>';
				
				echo '<tr class="capTabel">';
					echo '<td id="td_curs_invers" align="left"><b>'.LANG_Curs_valutar.'<br /> <span id="monedaFactura2">'. $currencies[$moneda_comanda] .'</span> -> '. $currencies[$moneda_comanda] .'</b></td>';
					echo '<td align="left"><input type="text" value="'.$factura->curs_2.'" name="curs_invers" id="curs_invers" class="input_number" '.$disabled.'/></td>';
                    echo '<td id="td_serie_ci" align="left"><b>'.LANG_Serie_CI.CT_OBLIGATORIU.'</b></td>';
                    echo '<td align="left"><input type="text" value="'.$factura->serie_ci_delegat.'" name="serie_ci" id="serie_ci"/></td>';
				echo '</tr>';
                
                echo '<tr class="capTabel">';
                    echo '<td id="td_text_factura" align="left"><b>'.LANG_Text_factura.CT_OBLIGATORIU.'</b></td>';
                    echo '<td align="left"><textarea maxlength="500" cols="30" rows="2" id="text_factura" name="text_factura">'.$factura->text_factura.'</textarea></td>';
                    echo '<td id="td_tva_inclus" align="left"><b>'.LANG_TVA.'</b></td>';
                    
                    if ($factura->tva_inclus == '1')
                        $check_tva_inclus_da = 'checked="checked"';
                    if ($factura->tva_inclus == '0')
                        $check_tva_inclus_nu = 'checked="checked"';
                        
                    echo '<td align="left"><input type="radio" name="tva_inclus" id="t1" value="1" '.$check_tva_inclus_da.'> '.LANG_TVA_inclus.'<br />
                            <input type="radio" name="tva_inclus" id="t2" value="0" '.$check_tva_inclus_nu.'> '.LANG_TVA_neinclus.'</td>';
                echo '</tr>';
				
				echo '<tr class="capTabel">';
					echo '<td id="td_serie_factura" align="left"><b>'.LANG_Serie_factura.CT_OBLIGATORIU.'</b></td>';
					echo '<td align="left"><input type="text" value="' . $serie . '" name="serie_factura" id="serie_factura" /></td>';
                    echo '<td id="td_tva" align="left"><b>'.LANG_Cota_TVA.'</b></td>';
                    echo '<td align="left"><input type="text" value="'.$factura->cota_tva.'" name="tva" id="tva" class="input_number"/>&nbsp;%</td>';
				echo '</tr>';                   
                
                echo '<tr class="capTabel">';
					echo "<td colspan=\"4\" align=\"center\">";
                        echo '<input type="button" name="print_factura" id="print_factura" value="' . LANG_Save_and_print_factura . '"' . "onclick=\"javascript: print_factura('". $id_comanda . "', true);\" />&nbsp;&nbsp;";
						echo '<input type="button" name="save_factura" id="save_factura" value="' . LANG_Save_factura . '"' . "onclick=\"javascript: print_factura('". $id_comanda . "', false);\" />&nbsp;&nbsp;";
                        echo '<input type="button" name="cancel" value="'.LANG_CANCEL.'" onclick="javascript: closeMessage();" />';						
					echo "</td>";
				echo '</tr>';
				echo output_extra_text_modal(LANG_EXTRA_MODAL_COMANDA_FACTURARE, 4);
			echo '</table>';	
		echo "</div>";		
		flush();
?>
